package com.hippo.core.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.ui.theme.ColorBorder
import com.hippo.core.ui.theme.ColorHint

@Composable
fun InputEdit2(
    modifier: Modifier = Modifier,
    text: String,
    hint: String,
    onValueChange: (String) -> Unit,
    enable: Boolean = true,
    isError: Boolean = false,
    rightPainter: Painter? = null,
    onRightClick: (() -> Unit)? = null,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
) {

    OutlinedTextField(
        modifier = modifier.fillMaxWidth().wrapContentHeight(),
        value = text,
        enabled = enable,
        isError = isError,
        onValueChange = onValueChange,
        placeholder = {
            Text(
                text = hint,
                style = MaterialTheme.typography.bodyMedium,
                color = ColorHint,
                fontSize= 14.sp,
                overflow = TextOverflow.Ellipsis,
            )
        },
        trailingIcon = {
            if (rightPainter != null) {
                Image(
                    modifier = Modifier.size(16.dp).click {
                        onRightClick?.invoke()
                    },
                    painter = rightPainter,
                    contentDescription = null
                )
            }
        },
        singleLine = true,
        colors = OutlinedTextFieldDefaults.colors(
            focusedBorderColor = MaterialTheme.colorScheme.primary,
            unfocusedBorderColor = ColorBorder,
            errorBorderColor = Color.Red,
            errorPlaceholderColor = Color.Red,
            disabledContainerColor = Color(0xFFF0F6FA)
        ),
        keyboardOptions = keyboardOptions,
        keyboardActions = keyboardActions
    )
}



@Preview(showBackground = true)
@Composable
private fun InputEditPreview() {
    Box(
        modifier = Modifier.padding(20.dp)
    ) {
        InputEdit2(
            text = "着急哦的着急哦的着急哦的着急哦的",
            hint = "Search",
            onValueChange = {},
            rightPainter = painterResource(R.mipmap.eye),
            onRightClick = {}
        )
    }
}